'''
Created on Mar 12, 2010

@author: Roni
'''

from cliques.potentialBasedSearch import potentialBasedSearch
import random
from cliques.closest.closestCliqueSearch import ClosestCliqueSearch




class OngoingMemberSearch(ClosestCliqueSearch):
    '''
    An anytime search algorithm that returns members with some wanted property
    '''
    
    def __init__(self):
        '''
        Constructor
        '''
        ClosestCliqueSearch.__init__(self)
        self.member_listeners = []
    
    def add_member_listener(self,listener):
        self.member_listeners.append(listener)

    def remove_member_listener(self,listener):
        self.member_listeners.remove(listener)
    
    def notify_member_listeners(self, event):
        for listener in self.member_listeners:
            listener.notify_member_found(event)
    
    def setup(self, unknown_graph, k, start_node):
        ClosestCliqueSearch.setup(self, unknown_graph, k, start_node)
        self.found_members = set()
                
    def notify_member_found(self,a_clique):
        ''' An event that is raised when a new k-clique has been found '''        
        for listener in self.clique_listeners:
            listener.notify_member_found(a_clique)
        self.found_members.update(a_clique) # Append a_clique members to found_clique_members

    def halt(self):
        ''' Search halts only when there are no more nodes to expand '''
        return len(self.generated)==0

